<<<<<<< HEAD
#include "user.h"
#include <QWidget>
#include <QMessageBox>
#include <qlineedit>
#include <QPushButton>
#include <QString>
using namespace std;
vector<user> users;
void addTask(const Task &task, user &User)
{
    // 在User用户中添加任务task
    User.usertask.push_back(task);
    FILE *outFile = fopen(User.filename.c_str(), "a+");
    if (outFile)
    {
        task.saveToFile(outFile);
        fclose(outFile);
    }
}
void deleteTask(int id, user &User,QWidget *par)
//在文件中找id的用户，没有找到，以par为父类弹出一个警告的消息对话框
{
    bool flag = false;
    for (int i = 0; i < int(User.usertask.size()); i++)
    {
        if (User.usertask[i].id == id)
        {
            User.usertask.erase(User.usertask.begin() + i);
            flag = true;
        }
    }
    if (!flag)
    {
        QMessageBox *cri=new QMessageBox;
        cri->setParent(par);
        cri->critical(par,"警告","文件中不存在此id");
        //cout << "failed,local stroage has no this id" << endl;
        return;
    }
    FILE *fp = fopen(User.filename.c_str(), "w");
    for (int j = 0; j < int(User.usertask.size()); j++)
    {
        User.usertask[j].saveToFile(fp);
    }
}
void deleteTask(string targetName, user &User,QWidget *par)
{
    // 在User用户对应的文件中删除任务，并在User用户中的Task中删除
    bool flag = false;
    for (int i = 0; i < int(User.usertask.size()); i++)
    {
        if (User.usertask[i].name == targetName)
        {
            User.usertask.erase(User.usertask.begin() + i);
            flag = true;
        }
    }
    if (!flag)
    {
        QMessageBox *cri=new QMessageBox;
        cri->setParent(par);
        cri->critical(par,"警告","文件中不存在此id");
        //cout << "failed,local stroage has no this id" << endl;
        return;
    }
    FILE *fp = fopen(User.filename.c_str(), "w");
    for (int j = 0; j < int(User.usertask.size()); j++)
    {
        User.usertask[j].saveToFile(fp);
    }
}

// 从文件加载任务
static vector<Task> loadFromFile(const char *filename)
{
    vector<Task> tasks;
    FILE *inFile = fopen(filename, "rb");
    if (!inFile)
        return tasks;
    Task task;
    int priority, type;
    string startTimeStr, remindTimeStr;
    while (fread(&task.id, sizeof(task.id), 1, inFile))
    {
        fread(&task.name, sizeof(task.name), 1, inFile);
        fread(&task.startTime, sizeof(task.startTime), 1, inFile);
        fread(&priority, sizeof(priority), 1, inFile);
        fread(&type, sizeof(type), 1, inFile);
        fread(&task.remindTime, sizeof(task.remindTime), 1, inFile);
        task.priorityLevel = static_cast<Priority>(priority);
        task.category = static_cast<Type>(type);
        tasks.push_back(task);
    }
    fclose(inFile);
    return tasks;
}

void loadTasks(user &User)
{
    User.usertask = loadFromFile("tasks.txt");
}
vector<user> loaduser(){//从users.txt文件中读取所有用户，返回向量
    vector<user> *tem_users=new vector<user>;
    FILE *inFile=fopen("users.txt","rb");
    if(!inFile)
        return *tem_users;
    user oneuser;
    string userid,filename;
    vector<int> usercode;
    while(fread(&oneuser.user_id,sizeof(oneuser.user_id),1,inFile)){
        fread(&oneuser.code,sizeof(oneuser.code),1,inFile);
        fread(&oneuser.filename,sizeof(oneuser.filename),1,inFile);
        tem_users->push_back(oneuser);
    }
    fclose(inFile);
    return *tem_users;

}
int find_user(string id,string code){
    vector<user> alluser=loaduser();
    vector<int> hash_code=user::get_hashcode(code);
    for(int i=0;i<int(alluser.size());i++){
        if(id==alluser[i].user_id&&hash_code==alluser[i].code) return i;
    }
    return -1;
}
void show_vectortask(vector<Task> tasks)
{
    // 基本输出任务，输出vector Task
    // 还没写好
}
void showPrioritytasks(user &User)
{
    vector<Task> htask, mtask, ltask;
    for (int i = 0; i < int(User.usertask.size()); i++)
    {
        switch (User.usertask[i].priorityLevel)
        {
        case 0:
            htask.push_back(User.usertask[i]);
            break;
        case 1:
            mtask.push_back(User.usertask[i]);
            break;
        case 2:
            ltask.push_back(User.usertask[i]);
            break;
        }
    }
    show_vectortask(htask);
    show_vectortask(mtask);
    show_vectortask(ltask);
}
void showDaytasks(user &User, int year, int month, int day)
{
    // 按天输出
    vector<Task> daytasks;
    for (int i = 0; i < int(User.usertask.size()); i++)
    {
        if (User.usertask[i].startTime.tm_year == year &&
            User.usertask[i].startTime.tm_mday == day &&
            User.usertask[i].startTime.tm_mon == month)
            daytasks.push_back(User.usertask[i]);
    }
    show_vectortask(daytasks);
}
void showMonthtasks(user &User, int year, int month)
{
    // 按月输出
    vector<Task> monthtasks;
    for (int i = 0; i < int(User.usertask.size()); i++)
    {
        if (User.usertask[i].startTime.tm_year == year &&
            User.usertask[i].startTime.tm_mon == month)
            monthtasks.push_back(User.usertask[i]);
    }
    show_vectortask(monthtasks);
}
void get_t(int &year, int &month, int &day)
{
    // 两个重载，分别为日和月的时间
    cout << "input year,month and day" << endl;
    cin >> year >> month >> day;
}
void get_t(int &year, int &month)
{
    cout << "input year,month" << endl;
    cin >> year >> month;
}
void showTasks(user &User, showType type)
{
    switch (type)
    {
    case PRIORITY:
        showPrioritytasks(User);
        break;
    case DAY:
    {
        int year, month, day;
        get_t(year, month, day);
        showDaytasks(User, year, month, day);
        break;
    }
    case MONTH:
    {
        int year, month;
        get_t(year, month);
        showMonthtasks(User, year, month);
        break;
    }
    }
}
void get_user_message(QLineEdit *line1,QLineEdit *line2,QPushButton *btn ) // 从命令行获得用户信息
/*从lineedit1和lineedit2获取两个字符串（用户名和密码）
 *在文件中检索，找到则进入下一个页面
 *没找到则弹出对话框，提示用户名或密码错误
 */
{
    QWidget *warning=new QWidget;//提示窗口
    QString this_id=line1->text();
    QString this_code=line2->text();
    //string cod=user::get_hashcode(this_code.toStdString());
    warning->move(300,300);
    warning->resize(100,100);
    warning->setWindowTitle("warning");
    warning->setWindowIconText("用户名或密码错误");
    vector<user> alluser=loaduser();
    if(find_user(this_id.toStdString(),this_code.toStdString())!=-1){
        change_to_next_ui();//还没写好的函数 变到下一个ui页面
    }
    else warning->show();


    // 未完待续
}
=======
#include "user.h"
using namespace std;
vector<user> users;
void addTask(const Task &task, user &User)
{
	// 在User用户中添加任务task
	User.usertask.push_back(task);
	FILE *outFile = fopen(User.filename.c_str(), "a+");
	if (outFile)
	{
		task.saveToFile(outFile);
		fclose(outFile);
	}
}
void deleteTask(int id, user &User)
{
	bool flag = false;
	for (int i = 0; i < User.usertask.size(); i++)
	{
		if (User.usertask[i].id == id)
		{
			User.usertask.erase(User.usertask.begin() + i);
			flag = true;
		}
	}
	if (!flag)
	{
		cout << "failed,local stroage has no this id" << endl;
		return;
	}
	FILE *fp = fopen(User.filename.c_str(), "w");
	for (int j = 0; j < User.usertask.size(); j++)
	{
		User.usertask[j].saveToFile(fp);
	}
}
void deleteTask(string targetName, user &User)
{
	// 在User用户对应的文件中删除任务，并在User用户中的Task中删除
	bool flag = false;
	for (int i = 0; i < User.usertask.size(); i++)
	{
		if (User.usertask[i].name == targetName)
		{
			User.usertask.erase(User.usertask.begin() + i);
			flag = true;
		}
	}
	if (!flag)
	{
		cout << "failed,local stroage has no this name" << endl;
		return;
	}
	FILE *fp = fopen(User.filename.c_str(), "w");
	for (int j = 0; j < User.usertask.size(); j++)
	{
		User.usertask[j].saveToFile(fp);
	}
}

// 从文件加载任务
static vector<Task> loadFromFile(const char *filename)
{
	vector<Task> tasks;
	FILE *inFile = fopen(filename, "rb");
	if (!inFile)
		return tasks;
	Task task;
	int priority, type;
	string startTimeStr, remindTimeStr;
	while (fread(&task.id, sizeof(task.id), 1, inFile))
	{
		fread(&task.name, sizeof(task.name), 1, inFile);
		fread(&task.startTime, sizeof(task.startTime), 1, inFile);
		fread(&priority, sizeof(priority), 1, inFile);
		fread(&type, sizeof(type), 1, inFile);
		fread(&task.remindTime, sizeof(task.remindTime), 1, inFile);
		task.priorityLevel = static_cast<Priority>(priority);
		task.category = static_cast<Type>(type);
		tasks.push_back(task);
	}
	fclose(inFile);
	return tasks;
}

void loadTasks(user &User)
{
	User.usertask = loadFromFile("tasks.txt");
}

void show_vectortask(vector<Task> tasks)
{
	// 基本输出任务，输出vector Task
	// 还没写好
}
void showPrioritytasks(user &User)
{
	vector<Task> htask, mtask, ltask;
	for (int i = 0; i < User.usertask.size(); i++)
	{
		switch (User.usertask[i].priorityLevel)
		{
		case 0:
			htask.push_back(User.usertask[i]);
			break;
		case 1:
			mtask.push_back(User.usertask[i]);
			break;
		case 2:
			ltask.push_back(User.usertask[i]);
			break;
		}
	}
	show_vectortask(htask);
	show_vectortask(mtask);
	show_vectortask(ltask);
}
void showDaytasks(user &User, int year, int month, int day)
{
	// 按天输出
	vector<Task> daytasks;
	for (int i = 0; i < User.usertask.size(); i++)
	{
		if (User.usertask[i].startTime.tm_year == year &&
			User.usertask[i].startTime.tm_mday == day &&
			User.usertask[i].startTime.tm_mon == month)
			daytasks.push_back(User.usertask[i]);
	}
	show_vectortask(daytasks);
}
void showMonthtasks(user &User, int year, int month)
{
	// 按月输出
	vector<Task> monthtasks;
	for (int i = 0; i < User.usertask.size(); i++)
	{
		if (User.usertask[i].startTime.tm_year == year &&
			User.usertask[i].startTime.tm_mon == month)
			monthtasks.push_back(User.usertask[i]);
	}
	show_vectortask(monthtasks);
}
void get_t(int &year, int &month, int &day)
{
	// 两个重载，分别为日和月的时间
	cout << "input year,month and day" << endl;
	cin >> year >> month >> day;
}
void get_t(int &year, int &month)
{
	cout << "input year,month" << endl;
	cin >> year >> month;
}
void showTasks(user &User, showType type)
{
	switch (type)
	{
	case PRIORITY:
		showPrioritytasks(User);
		break;
	case DAY:
	{
		int year, month, day;
		get_t(year, month, day);
		showDaytasks(User, year, month, day);
		break;
	}
	case MONTH:
	{
		int year, month;
		get_t(year, month);
		showMonthtasks(User, year, month);
		break;
	}
	}
}
int find_user(string id)
{
	// 在所有用户中查找该id
	for (int i = 0; i < users.size(); i++)
	{
		if (users[i].user_id == id)
			return i;
	}
	return -1;
}
void get_user_message() // 从命令行获得用户信息
{
	cout << "please input your id" << endl;
	int index = -1;
	string id, code;
	cin >> id;
	if ((index = find_user(id)) != -1)
	{
		cout << "please input your code" << endl;
		cin >> code;
		if (users[index].code == user::get_hashcode(code))
		{
			cout << users[index].filename << "are opened successfully" << endl;

			return;
		}
		cout << "error,your code is wrong!" << endl;
	}

	// 未完待续
}
>>>>>>> fc25350d42a2f4fb928ad55076097ca0dad100eb
